#! /bin/bash
workDir=$(cd $(dirname $0); pwd);
bDevOpsExist=true;
bOpsProxyExist=true;

#安装包禁止执行执行操作
if [ -f $workDir"/package/kingdevopscenter.kfz" ];
then
	echo "请在安装目录下执行uninstall.sh";
	exit;
fi

# echo color
SETCOLOR_SUCCESS="echo -en \\033[1;32m";
SETCOLOR_FAILURE="echo -en \\033[1;31m";
SETCOLOR_TIP="echo -en \\033[1;34m";
SETCOLOR_NORMAL="echo -en \\033[0;39m";

# echo function
function echoSuccess(){
	$SETCOLOR_SUCCESS;
	echo $1 $2 $3 $4 $5 $6;
	$SETCOLOR_NORMAL;
}
function echoFailure(){
	$SETCOLOR_FAILURE;
	echo $1 $2 $3 $4 $5 $6;
	$SETCOLOR_NORMAL;
}
function echoTip(){
	$SETCOLOR_TIP;
	echo $1 $2 $3 $4 $5 $6;
	$SETCOLOR_NORMAL;
}

function uninstallDevOpsCenter(){

	echo "开始卸载开发/运维中心";
	
	#删除进程
	licenseWatchDogCheck=$(ps -aux | grep "/kingdevopscenter3.6/exe/kinglicense/exe/watchDog" | awk "NR == 1" | awk '{print $11}');
	if  [ "$licenseWatchDogCheck" == "grep" ]
	then
		licenseWatchDogPid=$(ps -aux | grep "/kingdevopscenter3.6/exe/kinglicense/exe/watchDog" | awk "NR == 2" | awk '{print $2}');
	else
		licenseWatchDogPid=$(ps -aux | grep "/kingdevopscenter3.6/exe/kinglicense/exe/watchDog" | awk "NR == 1" | awk '{print $2}');
	fi
	if [ -n "$licenseWatchDogPid" ]
	then
		kill -9 $licenseWatchDogPid;
	fi
	
	licensePid=$(netstat -tunlp | grep :11007 | awk '{print $7}' | awk -F "/" '{printf $1}');
	if [ -n "$licensePid" ]
	then
		kill -9 $licensePid;
	fi
	
	# 删除容器
	check=$(sudo docker inspect kingdevopscenter | grep kingdevopscenter | wc -l);
	if [ $check -ne 0 ]
	then
		sudo docker stop kingdevopscenter;
		sudo docker rm -f kingdevopscenter;
	fi

	#删除elasticsearch
	check=$(sudo docker inspect elasticsearch | grep elasticsearch | wc -l);
	if [ $check -ne 0 ]
	then
		sudo docker stop elasticsearch;
		sudo docker rm -f elasticsearch;
	fi
	
	# 删除文件
	if [ "$bOpsProxyExist" == "false" ]
	then
		rm -rf $workDir/../config;
		echo "删除配置文件成功";
		rm -rf $workDir/../dockerimagedb;
		echo "删除镜像包成功";
		rm -rf $workDir/../dockersetup;
		echo "删除docker安装程序成功";
		rm -rf $workDir/../exedb;
		echo "删除执行程序库成功";
		rm -rf $workDir/../kingdevopscenter3.6;
		echo "删除KF3.6开发运维中心程序文件成功";
	else
		rm -rf $workDir/../config/devconfig.json;
		rm -rf $workDir/../config/oemConfig.json;
		rm -rf $workDir/../config/opscenterconfig.json;
		echo "删除配置文件成功";
		rm -rf $workDir/../kingdevopscenter3.6;
		echo "删除KF3.6开发运维中心程序文件成功";
	fi
	
	#删除开机自启动配置
	sudo chmod +x /etc/rc.d/rc.local;
	checkLicenseRc=$(sudo cat /etc/rc.d/rc.local | grep -n "\/kingdevopscenter3.6\/exe\/kinglicense\/exe\/serviceMonitor.sh" | awk -F ":" '{print $1}' | wc -l);
	if [ $checkLicenseRc -ne 0 ]
	then
		echo "删除授权服务看门狗配置";
		row=$(sudo cat /etc/rc.d/rc.local | grep -n "\/kingdevopscenter3.6\/exe\/kinglicense\/exe\/serviceMonitor.sh" | awk -F ":" '{print $1}' | sed -n '1p');
		sudo sed -i ""$row"c\\" /etc/rc.d/rc.local;
	else
		echo "删除失败，授权服务看门狗配置未找到";
	fi
	
	echo "开发/运维中心卸载成功, 工程文件已保留: $workDir/../sdb, 如非必要请勿手动删除工程文件";
}

function uninstallOpsProxy(){

	echo "开始卸载运维代理";
	
	# 删除进程
	opsProxyWatchDogCheck=$(ps -aux | grep "/kingopsproxy3.6/exe/kingguard/exe/watchDog" | awk "NR == 1" | awk '{print $11}');
	if  [ "$opsProxyWatchDogCheck" == "grep" ]
	then
		opsProxyWatchDogPid=$(ps -aux | grep "/kingopsproxy3.6/exe/kingguard/exe/watchDog" | awk "NR == 2" | awk '{print $2}');
	else
		opsProxyWatchDogPid=$(ps -aux | grep "/kingopsproxy3.6/exe/kingguard/exe/watchDog" | awk "NR == 1" | awk '{print $2}');
	fi
	if [ -n "$opsProxyWatchDogPid" ]
	then
		kill -9 $opsProxyWatchDogPid;
	fi
	
	filebeatCheck=$(ps -aux | grep "/kingopsproxy3.6/exe/kingfilebeat/filebeat" | awk "NR == 1" | awk '{print $11}');
	if  [ "$filebeatCheck" == "grep" ]
	then
		filebeatPid=$(ps -aux | grep "/kingopsproxy3.6/exe/kingfilebeat/filebeat" | awk "NR == 2" | awk '{print $2}');
	else
		filebeatPid=$(ps -aux | grep "/kingopsproxy3.6/exe/kingfilebeat/filebeat" | awk "NR == 1" | awk '{print $2}');
	fi
	if [ -n "$filebeatPid" ]
	then
		kill -9 $filebeatPid;
	fi
	
	metricbeatCheck=$(ps -aux | grep "/kingopsproxy3.6/exe/kingmetricbeat/metricbeat" | awk "NR == 1" | awk '{print $11}');
	if  [ "$metricbeatCheck" == "grep" ]
	then
		metricbeatPid=$(ps -aux | grep "/kingopsproxy3.6/exe/kingmetricbeat/metricbeat" | awk "NR == 2" | awk '{print $2}');
	else
		metricbeatPid=$(ps -aux | grep "/kingopsproxy3.6/exe/kingmetricbeat/metricbeat" | awk "NR == 1" | awk '{print $2}');
	fi
	if [ -n "$metricbeatPid" ]
	then
		kill -9 $metricbeatPid;
	fi
	
	opsProxyPid=$(netstat -tunlp | grep :13000 | awk '{print $7}' | awk -F "/" '{printf $1}');
	if [ -n "$opsProxyPid" ]
	then
		kill -9 $opsProxyPid;
	fi
	
	# 删除文件
	if [ "$bDevOpsExist" == "false" ]
	then
		rm -rf $workDir/../config;
		echo "删除配置文件成功";
		rm -rf $workDir/../dockerimagedb;
		echo "删除镜像包成功";
		rm -rf $workDir/../dockersetup;
		echo "删除docker安装程序成功";
		rm -rf $workDir/../exedb;
		echo "删除执行程序库成功";
		rm -rf $workDir/../kingopsproxy3.6;
		echo "删除运维代理程序文件成功";
	else
		rm -rf $workDir/../config/proxyconfig.json;
		echo "删除配置文件成功";
		rm -rf $workDir/../kingopsproxy3.6;
		echo "删除运维代理程序文件成功";
	fi
	
	#删除开机自启动配置
	sudo chmod +x /etc/rc.d/rc.local;
	checkOpsproxyRc=$(sudo cat /etc/rc.d/rc.local | grep -n "\/kingopsproxy3.6\/exe\/kingguard\/exe\/start.sh" | awk -F ":" '{print $1}' | wc -l);
	if [ $checkOpsproxyRc -ne 0 ]
	then
		echo "删除运维代理看门狗配置";
		row=$(sudo cat /etc/rc.d/rc.local | grep -n "\/kingopsproxy3.6\/exe\/kingguard\/exe\/start.sh" | awk -F ":" '{print $1}' | sed -n '1p');
		sudo sed -i ""$row"c\\" /etc/rc.d/rc.local;
	else
		echo "删除失败，运维代理看门狗配置未找到";
	fi
	
	echo "运维代理卸载成功, 工程文件已保留: $workDir/../sdb, 如非必要请勿手动删除工程文件";
}

# 选择安装的软件
if [ ! -d "$workDir/../kingdevopscenter3.6/" ]
then 
	bDevOpsExist=false;
fi

if [ ! -d "$workDir/../kingopsproxy3.6/" ]
then 
	bOpsProxyExist=false;
fi

if [[ "$bDevOpsExist" == "false" ]] && [[ "$bOpsProxyExist" == "false" ]]
then
	echo "未检测到KF3.6已经安装,卸载程序退出";
	exit;
elif [[ "$bDevOpsExist" == "true" ]] && [[ "$bOpsProxyExist" == "false" ]]
then
	# 只安装了开发运维中心
	echo "[1] 卸载开发/运维中心";
	echo "[E] 退出卸载程序";
	read -p "请输入您要进行的操作:" input;
	if [[ "$input" != "1" ]] && [[ "$input" != "E" ]] && [[ "$input" != "e" ]]
	then
		# 不识别的操作
		echoFailure "不识别的操作,卸载程序退出";
		exit;
	fi
elif [[ "$bDevOpsExist" == "false" ]] && [[ "$bOpsProxyExist" == "true" ]]
then
	# 只安装了运维代理
	echo "[2] 卸载运维代理";
	echo "[E] 退出卸载程序";
	read -p "请输入您要进行的操作:" input;
	if [[ "$input" != "2" ]] && [[ "$input" != "E" ]] && [[ "$input" != "e" ]]
	then
		# 不识别的操作
		echoFailure "不识别的操作,卸载程序退出";
		exit;
	fi
elif [[ "$bDevOpsExist" == "true" ]] && [[ "$bOpsProxyExist" == "true" ]]
then
	# 安装了开发运维中心和运维代理
	echo "[1] 卸载开发/运维中心";
	echo "[2] 卸载运维代理";
	echo "[E] 退出卸载程序";
	read -p "请输入您要进行的操作:" input;
	if [[ "$input" != "1" ]] && [[ "$input" != "2" ]] && [[ "$input" != "12" ]] && [[ "$input" != "E" ]] && [[ "$input" != "e" ]]
	then
		# 不识别的操作
		echoFailure "不识别的操作,卸载程序退出";
		exit;
	fi
fi

if [[ "$input" == "E" ]] || [[ "$input" == "e" ]]
then
	# 退出卸载程序
	echoTip "退出卸载程序";
	exit;
elif [ "$input" == "1" ]
then
	# 卸载开发运维中心
	read -p "确定要卸载开发运维中心? 开发运维中心文件也会被删除（工程文件保留）: Y[YES]  N[NO]: " input2;
	if [[ "$input2" == "Y" ]] || [[ "$input2" == "y" ]]
	then
		# 卸载开发运维中心,调用函数
		uninstallDevOpsCenter;
	elif [[ "$input2" == "N" ]] || [[ "$input2" == "n" ]]
	then
		exit;
	else
		# 不识别的操作
		echoFailure "不识别的操作,卸载程序退出";
		exit;
	fi
elif [ "$input" == "2" ]
then
	# 卸载运维代理
	read -p "确定要卸载运维代理? 运维代理文件也会被删除（工程文件保留）: Y[YES]  N[NO]: " input2;
	if [[ "$input2" == "Y" ]] || [[ "$input2" == "y" ]]
	then
		# 卸载运维代理,调用函数
		uninstallOpsProxy;
	elif [[ "$input2" == "N" ]] || [[ "$input2" == "n" ]]
	then
		exit;
	else
		# 不识别的操作
		echoFailure "不识别的操作,卸载程序退出";
		exit;
	fi
elif [ "$input" == "12" ]
then
	# 卸载开发运维中心,运维代理
	read -p "确定要卸载开发运维中心和运维代理? 开发运维中心和运维代理文件也会被删除（工程文件保留）: Y[YES]  N[NO]: " input2;
	if [[ "$input2" == "Y" ]] || [[ "$input2" == "y" ]]
	then
		# 卸载开发运维中心,调用函数
		uninstallDevOpsCenter;
		# 卸载运维代理,调用函数
		uninstallOpsProxy;
	elif [[ "$input2" == "N" ]] || [[ "$input2" == "n" ]]
	then
		exit;
	else
		# 不识别的操作
		echoFailure "不识别的操作,卸载程序退出";
		exit;
	fi
else
	# 不识别的操作
	echoFailure "不识别的操作,卸载程序退出";
	exit;
fi

# 删除基础镜像和setup
if [ ! -d "$workDir/../kingdevopscenter3.6/" ] && [ ! -d "$workDir/../kingopsproxy3.6/" ]
then
	check=$(sudo docker inspect kingdevopscenter | grep kingdevopscenter | wc -l);
	if [ $check -eq 0 ]
	then
		sudo docker rmi -f kingfusionbase;
	fi

	check=$(sudo docker inspect elasticsearch | grep elasticsearch | wc -l);
	if [ $check -eq 0 ]
	then
		sudo docker rmi -f elasticsearch:7.8.0;
	fi

	rm -rf $workDir/../config;
	rm -rf $workDir/../exedb;
	rm -rf $workDir;
fi
